<?php
namespace wechat;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015-10-01
 * Time: 15:41
 */
/**
 * * Created by PhpStorm.
 * * User: bin
 * * Date: 15-1-16
 * * Time: 上午9:48
 * */
 
//根据网上的代码 改写 不需要支持和 libconv 支持
//namespace HomeCommon;
// 微信处理类
 
set_time_limit(30);
 
 
class Weixin
{
//构造方法
 
    static $qrcode_url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?";
 
    static $token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&";
 
    static $qrcode_get_url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?";
 
   //生成二维码
    //$wechat = array('appid'=>'','secret'=>'' );
    public function getEwm($wechat, $fqid, $type = 1)
    {
        //var_dump($wechat,$fqid);exit;
        //$wechat = array('appid'=>'','secret'=>'' );
 
        $appid = $wechat['appid'];
 
        $secret = $wechat['appsecret'];
 
        $ACCESS_TOKEN = $this->getToken($appid, $secret);
 
        $url = $this->getQrcodeurl($ACCESS_TOKEN, $fqid, 1);
        return $url;
        //下载到本地时使用
        //return $this->DownLoadQr($url, time());
 
    }
 
    protected function getQrcodeurl($ACCESS_TOKEN, $fqid, $type = 1)
    {
 
        $url = self::$qrcode_url . 'access_token=' . $ACCESS_TOKEN;
        //echo $url;
 
        if ($type == 1) {
            //生成永久二维码
 
            $qrcode = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": ' . $fqid . '}}}';
 
        } else {
            //生成临时二维码
 
            $qrcode = '{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": ' . $fqid . '}}}';
 
        }
 
        $result = $this->http_post_data($url, $qrcode);
 
        //print_r($result);
        $oo = json_decode($result[1]);
 
        if (!$oo->ticket) {
 
            $this->ErrorLogger('getQrcodeurl falied. Error Info: getQrcodeurl get failed');
 
            exit();
 
        }
 
        $url = self::$qrcode_get_url . 'ticket=' . $oo->ticket . '';
 
        return $url;
 
    }
 
    protected function getToken($appid, $secret)
    {
        //echo self::$token_url . "appid=$appid&secret=$secret";
        $data = json_decode(file_get_contents("access_token.json"));
        if ($data->expire_time < time()) {
            $ACCESS_TOKEN = $this->curl_get_contents(self::$token_url . "appid=$appid&secret=$secret");
 
            $ACCESS_TOKEN = json_decode($ACCESS_TOKEN);
            if($ACCESS_TOKEN){
                //print_r($ACCESS_TOKEN);
                $ACCESS_TOKEN = $ACCESS_TOKEN->access_token;
                $data->expire_time = time() + 7000;
                $data->access_token = $ACCESS_TOKEN;
                $fp = fopen("access_token.json", "w");
                fwrite($fp, json_encode($data));
                fclose($fp);
            }else{
    
                $ACCESS_TOKEN = '';
            }
        }else{
            $ACCESS_TOKEN = $data->access_token;
        }
        return $ACCESS_TOKEN;
 
    }
 
    protected function http_post_data($url, $data_string)
    {
 
        //echo $url;
        //echo $data_string;
 
        $ch = curl_init();
 
        curl_setopt($ch, CURLOPT_POST, 1);
 
        curl_setopt($ch, CURLOPT_URL, $url);
 
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
 
        @curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
 
 
 
 
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($data_string))
        );
 
 
 
        //echo curl_exec($ch);
        ob_start();
        echo curl_exec($ch);
        if (curl_errno($ch)) {
 
            $this->ErrorLogger('curl falied. Error Info: ' . curl_error($ch));
 
        }
        $return_content = ob_get_contents();
        ob_end_clean();
 
        //exit();
        $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //注意curl_getinfo 要在curl_exec 后面运行，否则都返回0
        //echo $return_code;
        return array($return_code, $return_content);
    }
 
    //下载二维码到服务器
    protected function DownLoadQr($url, $filestring)
    {
 
        echo '二维码 url' . $url;
        if ($url == "") {
 
            return false;
 
        }
 
        $filename = $filestring . '.jpg';
 
        //ob_start();
        //echo readfile($url);
        //$img = ob_get_contents();
        //ob_end_clean();
        //$size = strlen($img);
        $img = $this->curl_get_contents($url);
        $size = strlen($img);
        $fp2 = fopen('./' . $filename, "a");
 
        if (fwrite($fp2, $img) === false) {
 
            $this->ErrorLogger('dolwload image falied. Error Info: 无法写入图片');
 
            exit();
 
        }
 
        fclose($fp2);
 
        return './' . $filename;   //./Uploads/qrcode/
 
    }
 
    private function ErrorLogger($errMsg)
    {
 
        $logger = fopen('./ErrorLog.txt', 'a+');
 
        fwrite($logger, date('Y-m-d H:i:s') . " Error Info : " . $errMsg . "rn");
 
    }
 
    protected function curl_get_contents($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 3);
        curl_setopt($ch, CURLOPT_USERAGENT, '');
        curl_setopt($ch, CURLOPT_REFERER,'');
        @curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        $r = curl_exec($ch);
        curl_close($ch);
        return $r;
    }
 
 
 
}
 
// 演示代码
 

 
 
//http://weixin.qq.com/q/aj8LDoLm9L_NGznHoxOY
 
 
 
 
 
/*
 *
 * 一、场景二维码
为了满足用户渠道推广分析的需要，公众平台提供了生成带参数二维码的接口。使用该接口可以获得多个带不同场景值的二维码，用户扫描后，公众号可以接收到事件推送。
目前有2种类型的二维码，分别是临时二维码和永久二维码，前者有过期时间，最大为1800秒，但能够生成较多数量，后者无过期时间，数量较少（目前参数只支持1--100000）。两种二维码分别适用于帐号绑定、用户来源统计等场景。
用户扫描带场景值二维码时，可能推送以下两种事件：
如果用户还未关注公众号，则用户可以关注公众号，关注后微信会将带场景值关注事件推送给开发者。
如果用户已经关注公众号，在用户扫描后会自动进入会话，微信也会将带场景值扫描事件推送给开发者。
获取带参数的二维码的过程包括两步，首先创建二维码ticket，然后凭借ticket到指定URL换取二维码。
二、创建二维码ticket
每次创建二维码ticket需要提供一个开发者自行设定的参数（scene_id），分别介绍临时二维码和永久二维码的创建二维码ticket过程。
临时二维码请求说明
http请求URL
https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN
POST数据格式
1 {
2 "expire_seconds": 1800,
3 "action_name": "QR_SCENE",
4 "action_info": {
5 "scene": {
6 "scene_id": 100000
7 }
8 }
9 }
返回格式：
{
"ticket": "gQFK8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL3kweXE0T3JscWY3UTltc3ZPMklvAAIEG9jUUgMECAcAAA==",
"expire_seconds": 1800
}
永久二维码请求说明
http请求URL
https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN
POST数据格式
{
"action_name": "QR_LIMIT_SCENE",
"action_info": {
"scene": {
"scene_id": 1000
}
}
}
返回格式
{
"ticket": "gQHi8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0UweTNxNi1sdlA3RklyRnNKbUFvAAIELdnUUgMEAAAAAA=="
}
提交数据参数说明：
返回结果参数说明
程序实现
三、通过ticket换取二维码
获取二维码ticket后，开发者可用ticket换取二维码图片。无须登录态即可调用。
HTTPS GET请求说明（TICKET必需UrlEncode）
https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET
ticket正确情况下，http 返回码是200，是一张图片，可以直接展示或者下载。
HTTP头示例如下：
1 {
2 "url": "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQHi8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0UweTNxNi1sdlA3RklyRnNKbUFvAAIELdnUUgMEAAAAAA%3D%3D",
3 "content_type": "image/jpg",
4 "http_code": 200,
5 "header_size": 162,
6 "request_size": 181,
7 "filetime": -1,
8 "ssl_verify_result": 20,
9 "redirect_count": 0,
10 "total_time": 0.509,
11 "namelookup_time": 0,
12 "connect_time": 0.058,
13 "pretransfer_time": 0.343,
14 "size_upload": 0,
15 "size_download": 28497,
16 "speed_download": 55986,
17 "speed_upload": 0,
18 "download_content_length": 28497,
19 "upload_content_length": 0,
20 "starttransfer_time": 0.481,
21 "redirect_time": 0
22 }
四、扫描带参数二维码事件
用户扫描带场景值二维码时，可能推送以下两种事件：
如果用户还未关注公众号，则用户可以关注公众号，关注后微信会将带场景值关注事件推送给开发者。
如果用户已经关注公众号，则微信会将带场景值扫描事件推送给开发者。
1. 用户未关注时，进行关注后的事件推送
<xml>
<ToUserName><![CDATA[gh_45072270791c]]></ToUserName>
<FromUserName><![CDATA[o7Lp5t6n59DeX3U0C7Kric9qEx-Q]]></FromUserName>
<CreateTime>1389684286</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[subscribe]]></Event>
<EventKey><![CDATA[qrscene_1000]]></EventKey>
<Ticket><![CDATA[gQHi8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0UweTNxNi1sdlA3RklyRnNKbUFvAAIELdnUUgMEAAAAAA==]]></Ticket>
</xml>
参数说明
2. 用户已关注时的事件推送
<xml>
<ToUserName><![CDATA[gh_45072270791c]]></ToUserName>
<FromUserName><![CDATA[o7Lp5t6n59DeX3U0C7Kric9qEx-Q]]></FromUserName>
<CreateTime>1389684184</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[SCAN]]></Event>
<EventKey><![CDATA[1000]]></EventKey>
<Ticket><![CDATA[gQHi8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0UweTNxNi1sdlA3RklyRnNKbUFvAAIELdnUUgMEAAAAAA==]]></Ticket>
</xml>
参数说明
事件代码：
以下代码判定了扫描带参数二维码的两种情形
private function receiveEvent($object)
{
$contentStr = "";
switch ($object->Event)
{
case "subscribe":
$contentStr = "欢迎关注方倍工作室 ";
if (isset($object->EventKey)){
$contentStr = "关注二维码场景 ".$object->EventKey;
}
break;
case "SCAN":
$contentStr = "扫描 ".$object->EventKey;
//要实现统计分析，则需要扫描事件写入数据库，这里可以记录 EventKey及用户OpenID，扫描时间
break;
default:
break;
}
$resultStr = $this->transmitText($object, $contentStr);
return $resultStr;
}
如果您不懂微信第三方开发，还是找人做吧
 *
 * */

?>